package com.yinhe.music.yhmusic.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.login.validcode.WebActivity;
import com.yinhe.music.yhmusic.utils.UpdateUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * @author 何晓华
 * @date 2017/9/21
 */

public class AboutActivity extends BaseActivity {
    @BindView(R.id.update_layout)
    LinearLayout updateLayout;
    @BindView(R.id.evaluate_layout)
    LinearLayout evaluateLayout;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.version)
    TextView version;

    Disposable disposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);

        setToolbar("关于");

        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startUserAuth();
            }
        });

        updateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkUpdate();
            }
        });

        evaluateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                evaluate();
            }
        });

        version.setText(String.format("当前版本 V%s", UpdateUtils.getAppVersionName(this)));
    }

    private void startUserAuth() {
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", IConstants.USER_AUTH_URL);
        intent.putExtra("title", getString(R.string.user_auth_title));
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
        super.onDestroy();
    }

    private void checkUpdate() {
        disposable = UpdateUtils.checkUpdate(this, true);
    }

    private void evaluate() {
        UpdateUtils.goToMarket(this, getPackageName());
    }

}
